// http://stackoverflow.com/questions/5375817/android-pinch-zoom
// http://stackoverflow.com/questions/10013906/android-zoom-in-out-relativelayout-with-spread-pinch

package org.example.touch;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Relative extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

class OnPinchListener extends SimpleOnScaleGestureListener {

	float startingSpan;
	float endSpan;
	float startFocusX;
	float startFocusY;

	public boolean onScaleBegin(ScaleGestureDetector detector) {
		startingSpan = detector.getCurrentSpan();
		startFocusX = detector.getFocusX();
		startFocusY = detector.getFocusY();
		return true;
	}

	/*
	 * public boolean onScale(ScaleGestureDetector detector) {
	 * mZoomableRelativeLayout.scale(detector.getCurrentSpan() / startingSpan,
	 * startFocusX, startFocusY); return true; }
	 * 
	 * public void onScaleEnd(ScaleGestureDetector detector) {
	 * mZoomableRelativeLayout.restore(); }
	 */
}

class ZoomableRelativeLayout extends RelativeLayout {
	float mScaleFactor = 1;
	float mPivotX;
	float mPivotY;

	public ZoomableRelativeLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public ZoomableRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public ZoomableRelativeLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	protected void dispatchDraw(Canvas canvas) {
		canvas.save(Canvas.MATRIX_SAVE_FLAG);
		canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);
		super.dispatchDraw(canvas);
		canvas.restore();
	}

	public void scale(float scaleFactor, float pivotX, float pivotY) {
		mScaleFactor = scaleFactor;
		mPivotX = pivotX;
		mPivotY = pivotY;
		this.invalidate();
	}

	public void restore() {
		mScaleFactor = 1;
		this.invalidate();
	}

}